﻿describe("Strategies/Burma", function () {

    var burma;

    beforeEach(function () {
        burma = new Strategies.Burma();
    });

    describe("x-wing (2*2) - should find by:", function () {

        it("row", function () {

            var grid = "--54--6-2--6-2-15-293561784-523-48--3-12-64-5----5732--3--4256--24--59--5-7--924-";
            var gridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(gridModel);

            //do
            var performResult = burma.perform(gridModel);

            //expect
            expect(performResult).toBeTruthy();
            expect(performResult.candidate).toEqual(9);
            expect(performResult.burmaSize).toEqual(2);
            var expectedContainingCells = [gridModel.cells[0][4], gridModel.cells[0][7],
                                            gridModel.cells[4][4], gridModel.cells[4][7]];
            expect(performResult.containingCells).toEqual(jasmine.arrayContaining(expectedContainingCells));
            var expectedTargetCells = [gridModel.cells[3][4], gridModel.cells[3][7]];
            expect(performResult.targetCells).toEqual(jasmine.arrayContaining(expectedTargetCells));

            var expectedGridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(expectedGridModel);
            expectedGridModel.cells[3][4].candidates.remove(9);
            expectedGridModel.cells[3][7].candidates.remove(9);
            expect(gridModel).toEqual(expectedGridModel);
        });

        xit(" is a swordfish :( column", function () {

            var grid = "-3---9-4-9628-45-1-7--5--9--83-976---4--68---69-1--8--32---518-85-9---62-1--8--53";
            var gridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(gridModel);

            //do
            var performResult = burma.perform(gridModel);

            //expect
            expect(performResult).toBeTruthy();
            expect(performResult.candidate).toEqual(3);
            expect(performResult.burmaSize).toEqual(3);
            var expectedContainingCells = [gridModel.cells[0][4], gridModel.cells[0][7],
                                            gridModel.cells[4][4], gridModel.cells[4][7]];
            expect(performResult.containingCells).toEqual(jasmine.arrayContaining(expectedContainingCells));
            var expectedTargetCells = [gridModel.cells[2][5], gridModel.cells[4][7]];
            expect(performResult.targetCells).toEqual(jasmine.arrayContaining(expectedTargetCells));

            var expectedGridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(expectedGridModel);
            expectedGridModel.cells[2][5].candidates.remove(3);
            expectedGridModel.cells[4][7].candidates.remove(3);
            expect(gridModel).toEqual(expectedGridModel);
        });

    });

    describe("swordfish (3*3) - should find by:", function () {

        it("row", function () {

            var grid = "1---9---5-8-4-6-9--97--56---7-548-2-2--96---4-4-1-2-5---9---8---2-6-951-7---5---9";
            var gridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(gridModel);

            //do
            var performResult = burma.perform(gridModel);

            //expect
            expect(performResult).toBeTruthy();
            expect(performResult.candidate).toEqual(7);
            expect(performResult.burmaSize).toEqual(3);
            var expectedContainingCells = [gridModel.cells[1][4], gridModel.cells[1][6], gridModel.cells[1][8],
                                            gridModel.cells[5][4], gridModel.cells[5][6], gridModel.cells[5][8],
                                            gridModel.cells[7][4],/*                  */ gridModel.cells[7][8]];
            expect(performResult.containingCells).toEqual(jasmine.arrayContaining(expectedContainingCells));
            var expectedTargetCells = [gridModel.cells[0][6], gridModel.cells[4][6], gridModel.cells[6][4], gridModel.cells[6][8]];
            expect(performResult.targetCells).toEqual(jasmine.arrayContaining(expectedTargetCells));

            var expectedGridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(expectedGridModel);
            expectedGridModel.cells[0][6].candidates.remove(7);
            expectedGridModel.cells[4][6].candidates.remove(7);
            expectedGridModel.cells[6][4].candidates.remove(7);
            expectedGridModel.cells[6][8].candidates.remove(7);

            expect(gridModel).toEqual(expectedGridModel);
        });

        it("column", function () {

            var grid = "3856-1-9-1-95------2--3951----3-5---53--1--69---4-----917253-8-2-31--9---5-9-7132";
            var gridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(gridModel);

            //do
            var performResult = burma.perform(gridModel);

            //expect
            expect(performResult).toBeTruthy();
            expect(performResult.candidate).toEqual(4);
            expect(performResult.burmaSize).toEqual(3);
            var expectedContainingCells = [gridModel.cells[1][1], gridModel.cells[1][5], gridModel.cells[3][7],
                                            gridModel.cells[3][1], gridModel.cells[7][5],
                                            gridModel.cells[7][1],/*                    */ gridModel.cells[7][7]];
            expect(performResult.containingCells).toEqual(jasmine.arrayContaining(expectedContainingCells));
            var expectedTargetCells = [gridModel.cells[1][4], gridModel.cells[1][6], gridModel.cells[1][8],
                                        gridModel.cells[3][0], gridModel.cells[3][2], gridModel.cells[3][6],
                                        gridModel.cells[7][4], gridModel.cells[7][8]];
            expect(performResult.targetCells).toEqual(jasmine.arrayContaining(expectedTargetCells));

            var expectedGridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(expectedGridModel);
            expectedGridModel.cells[1][4].candidates.remove(4);
            expectedGridModel.cells[1][6].candidates.remove(4);
            expectedGridModel.cells[1][8].candidates.remove(4);
            expectedGridModel.cells[3][0].candidates.remove(4);
            expectedGridModel.cells[3][2].candidates.remove(4);
            expectedGridModel.cells[3][6].candidates.remove(4);
            expectedGridModel.cells[3][8].candidates.remove(4);
            expectedGridModel.cells[7][4].candidates.remove(4);
            expectedGridModel.cells[7][8].candidates.remove(4);

            expect(gridModel).toEqual(expectedGridModel);
        });

    });

    describe("jellyfish (4*4) - should find by:", function () {

        it("row", function () {

            var grid = "--17538---5------77--89-1-----6-157-625478931-179-54------67--4-7-----1---63-97--";
            var gridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(gridModel);

            //do
            var performResult = burma.perform(gridModel);

            //expect
            expect(performResult).toBeTruthy();
            expect(performResult.candidate).toEqual(2);
            expect(performResult.burmaSize).toEqual(4);
            var expectedContainingCells = [gridModel.cells[0][0],/*                   */gridModel.cells[0][7], gridModel.cells[0][8],
                                                                gridModel.cells[3][4],/*                   */gridModel.cells[3][8],
                                                                gridModel.cells[5][4], gridModel.cells[5][7], gridModel.cells[5][8],
                                          gridModel.cells[8][0], gridModel.cells[8][4], gridModel.cells[8][7], gridModel.cells[8][8]];
            expect(performResult.containingCells).toEqual(jasmine.arrayContaining(expectedContainingCells));
            var expectedTargetCells = [gridModel.cells[1][0], gridModel.cells[1][4], gridModel.cells[1][7],
                                        gridModel.cells[2][7], gridModel.cells[2][8],
                                        gridModel.cells[6][0], gridModel.cells[6][7],
                                        gridModel.cells[7][0], gridModel.cells[7][4], gridModel.cells[7][8]];
            expect(performResult.targetCells).toEqual(jasmine.arrayContaining(expectedTargetCells));

            var expectedGridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(expectedGridModel);
            expectedGridModel.cells[1][0].candidates.remove(2);
            expectedGridModel.cells[1][4].candidates.remove(2);
            expectedGridModel.cells[1][7].candidates.remove(2);
            expectedGridModel.cells[2][7].candidates.remove(2);
            expectedGridModel.cells[2][8].candidates.remove(2);
            expectedGridModel.cells[6][0].candidates.remove(2);
            expectedGridModel.cells[6][7].candidates.remove(2);
            expectedGridModel.cells[7][0].candidates.remove(2);
            expectedGridModel.cells[7][4].candidates.remove(2);
            expectedGridModel.cells[7][8].candidates.remove(2);

            expect(gridModel).toEqual(expectedGridModel);
        });
    });
});

